﻿using System;
using System.IO.Packaging;
using System.Text;
using System.Windows.Controls;
using System.Xml;
using System.Windows.Documents;
using System.Windows;

namespace UIPrototyper
{
    public class CustomButton : Button, ISelectable, IXML, IChanged, ICopy, IDocument
    {
        Property property;
        private string description;

        public CustomButton()
        {
            property = new ButtonProperties(this);

            ((ButtonProperties)property).iChanged += new ChangedHandler(property_iChanged);
        }

        void property_iChanged(IChanged changedObject)
        {
            LaunchChangedEvent(changedObject);
        }

        #region ISelectable Members

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public Property Properties
        {
            get { return this.property; }
        }

        #endregion

        #region IXML Members

        public string GenerateXML(Package p)
        {
            StringBuilder xml = new StringBuilder();

            xml.Append("<Button description=\"");
            xml.Append(this.Description);
            xml.Append("\" ");

            xml.Append("margin=\"");
            xml.Append(this.Margin.ToString());
            xml.Append("\" ");

            xml.Append("content=\"");
            xml.Append(this.Content.ToString());
            xml.Append("\" ");

            xml.Append("height=\"");
            xml.Append(this.Height);
            xml.Append("\" ");

            xml.Append("width=\"");
            xml.Append(this.Width);
            xml.Append("\" ");

            xml.Append("screenID=\"");
            xml.Append(((ButtonProperties)this.Properties).ScreenID);
            xml.Append("\"/>");

            return xml.ToString();
        }

        public void LoadXML(XmlNode pXML)
        {
            this.Description = pXML.Attributes["description"].Value;
            this.Content = pXML.Attributes["content"].Value;
            this.Margin = MarginToThicknessConverter.ConvertToThickness(pXML.Attributes["margin"].Value);
            this.Height = Convert.ToDouble(pXML.Attributes["height"].Value.Replace('.', ','));
            this.Width = Convert.ToDouble(pXML.Attributes["width"].Value.Replace('.', ','));

            ((ButtonProperties)this.Properties).ScreenID = pXML.Attributes["screenID"].Value;
        }
        #endregion

        #region IChanged Members

        public event ChangedHandler iChanged;

        public void LaunchChangedEvent(IChanged pChangedObject)
        {
            if (iChanged != null)
            {
                iChanged(pChangedObject);
            }
        }

        #endregion

        #region ICopy Members

        public ISelectable Copy()
        {
            CustomButton myCopy = new CustomButton();

            myCopy.Description = this.description;

            myCopy.Margin = new System.Windows.Thickness(this.Margin.Left, this.Margin.Top, this.Margin.Right, this.Margin.Bottom);
            myCopy.Height = this.Height;
            myCopy.Width = this.Width;
            myCopy.Content = this.Content.ToString();

            ((ButtonProperties)myCopy.property).Tela = ((ButtonProperties)this.property).Tela;
            ((ButtonProperties)myCopy.property).ScreenID = ((ButtonProperties)this.property).ScreenID;
            ((ButtonProperties)myCopy.property).Texto = ((ButtonProperties)this.property).Texto;

            return myCopy;
        }

        #endregion

        #region IDocument Members

        public void Document(ref System.Windows.Documents.FlowDocument pDocument)
        {
            Paragraph controlDescription = new Paragraph();

            Run nameTitle = new Run("Nome: ");
            nameTitle.FontWeight = FontWeights.Bold;
            nameTitle.FontSize = 11;
            controlDescription.Inlines.Add(nameTitle);

            Run nameText = new Run(this.Name);
            nameText.FontSize = 11;
            controlDescription.Inlines.Add(nameText);
            controlDescription.Inlines.Add(new LineBreak());
            
            Run typeTitle = new Run("Texto: ");
            typeTitle.FontWeight = FontWeights.Bold;
            typeTitle.FontSize = 11;
            controlDescription.Inlines.Add(typeTitle);

            Run typeText = new Run(this.Content.ToString());
            typeText.FontSize = 11;
            controlDescription.Inlines.Add(typeText);
            controlDescription.Inlines.Add(new LineBreak());

            Run destinyTitle = new Run("Destino: ");
            destinyTitle.FontWeight = FontWeights.Bold;
            destinyTitle.FontSize = 11;
            controlDescription.Inlines.Add(destinyTitle);

            if (!string.IsNullOrEmpty(((ButtonProperties)property).ScreenID))
            {
                Run destinyText = new Run(ProjectSingleton.Project.Screens[((ButtonProperties)property).ScreenID].Name);
                destinyText.FontSize = 11;
                controlDescription.Inlines.Add(destinyText);

            }
            controlDescription.Inlines.Add(new LineBreak());

            Run descriptionTitle = new Run("Descrição: ");
            descriptionTitle.FontWeight = FontWeights.Bold;
            descriptionTitle.FontSize = 11;
            controlDescription.Inlines.Add(descriptionTitle);

            Run descriptionText = new Run(this.Description);
            descriptionText.FontSize = 11;
            controlDescription.Inlines.Add(descriptionText);
            controlDescription.Inlines.Add(new LineBreak());

            pDocument.Blocks.Add(controlDescription);
        }

        #endregion
    }
}
